package com.wjm.data_handle.controller;

import com.wjm.data_handle.service.XMLHandleService;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

@CrossOrigin({"http://localhost:8081"})
@Controller
public class HandleController {

    private XMLHandleService xmlHandleService;
    public HandleController(XMLHandleService xmlHandleService){
        this.xmlHandleService=xmlHandleService;
    }
    @ResponseBody
    @PostMapping("/upload")
    public Map<String,String> handleUploadFile(@RequestParam("file") MultipartFile uploadFile) throws IOException, DocumentException {
        Map<String, String> map = new HashMap<>( );
        if(uploadFile.isEmpty()){
            System.out.println("null" );
            map.put("error","The upload file is null");
            return map;
        }
        InputStream inputStream = uploadFile.getInputStream( );
        SAXReader saxReader = new SAXReader( );
        Document doc = saxReader.read(inputStream);
        String s = xmlHandleService.parseXML1(doc);
        map.put("parameter",s);
        String s1 = xmlHandleService.parseXML2(doc);
        map.put("wavelength",s1);
        return map;
    }
    @ResponseBody
    @CrossOrigin({"http://localhost:8081"})
    @GetMapping("/x")
    public String x(){
        return "xxxxx";
    }
}
